package com.atguigu.rabitmq.consumer.RabbitListenerAnnotation;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
public class ConsumerListenerAnnotation {

    /**
     * 使用@RabbitListener注解创建消息队列、交换机和队列交换机的绑定关系
     * 参数：bindings：绑定关系
     * 注解：@QueueBinding：队列绑定关系注解
     * - 参数：value：消息队列
     * - 注解：@Queue：消息队列注解
     * -- 参数：value：消息队列名称
     * -- 参数：durable：消息队列持久化
     * -- 参数：arguments（更多参数）的 @Argument 注解：存储信息过期时间
     * - 参数：exchange：交换机
     * - 注解：@Exchange 交换机注解
     * -- 参数：value：交换机名称
     * -- 参数：durable：交换机持久化
     * -- 参数：type：交换机类型
     * - 参数：key：路由的key
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "direct_queue_test_01",
                    durable = "true"
                    // arguments = {@Argument(name = "x-message-ttl", value = "100000", type = "java.lang.long")}
            ),
            exchange = @Exchange(
                    value = "direct_exchange_test",
                    durable = "true",
                    type = ExchangeTypes.DIRECT
            ),
            key = {"error"}
    ))
    public void consumer01(Message message) {
        byte[] body = message.getBody();
        String msg = new String(body);
        System.out.println(Thread.currentThread().getName() + " -- direct_queue_test_01 msg：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "direct_queue_test_02",
                    durable = "true"
            ),
            exchange = @Exchange(
                    value = "direct_exchange_test",
                    durable = "true",
                    type = ExchangeTypes.DIRECT
            ),
            key = {"error"}
    ))
    public void consumer02(Message message) {
        byte[] body = message.getBody();
        String msg = new String(body);
        System.out.println(Thread.currentThread().getName() + " -- direct_queue_test_02 msg：" + msg);
    }
}